use "/Users/jrthornton/Desktop/POBE 2021 replication/Data/anes_timeseries_cdf.dta" , replace
graph drop _all

* year of study
gen year = VCF0004
drop if year<1999

svyset [pw=VCF0009z]

set more off

******************
* SOPHISTICATION *
******************
* Created with three items: knowledge, interest, and involvement
		
* Interviewer Knowledge
gen interviewer_assessment = VCF0050a
recode interviewer_assessment (9=.) 	(1=5) (5=1) (2=4) (4=2)

* Interest
gen interest = VCF0310
recode interest (9=.) (0=.)

* Involvement 
gen involvement= VCF0723
recode involvement (0=.)

* Knowledge of house
gen house_before  = VCF0729
recode house_before (1=0) (2=1) (0=.)

* Scale from IRT with interviewer assessment
irt grm interviewer_assessment  interest  involvement if year>2005
predict soph_irt, latent
replace soph_irt = . if interviewer_assessment==.
replace soph_irt = . if interest == .
replace soph_irt = . if involvement == .
su soph_irt, meanonly
replace soph_irt = (soph_irt- r(min))/(r(max) - r(min))

* Split sample into thirds:
xtile soph_cat = soph_irt, nq(3)

* Alternate scale from IRT replaceing interviewer_assessment with knowledge of House
 irt hybrid (grm interest involvement ) (1pl house)
predict soph_irt_alt, latent
replace soph_irt_alt = . if house_before==.
replace soph_irt_alt = . if interest == .
replace soph_irt_alt = . if involvement == .
su soph_irt_alt, meanonly
replace soph_irt_alt = (soph_irt_alt- r(min))/(r(max) - r(min))
xtile soph_cat_alt = soph_irt_alt, nq(3)

* Four item measure:
irt hybrid (grm interest involvement interviewer_assess) (1pl house)
predict soph_irt_alt2, latent
replace soph_irt_alt2 = . if house_before==.
replace soph_irt_alt2 = . if interest == .
replace soph_irt_alt2 = . if involvement == .
su soph_irt_alt2, meanonly
replace soph_irt_alt2 = (soph_irt_alt2- r(min))/(r(max) - r(min))

* Three item additive index of main scale
su interviewer_assessment, meanonly
replace interviewer_assessment = (interviewer_assessment- r(min)) ///
	/(r(max) - r(min))
su interest, meanonly
replace interest = (interest- r(min))/(r(max) - r(min))
su involvement, meanonly
replace involvement = (involvement- r(min))/(r(max) - r(min))

pca interviewer_assessment  interest  involvement
alpha interviewer_assessment  interest  involvement
predict d1
gen sophistication = (interviewer_assessment + interest + involvement)/3
gen sophistication_alt = (house_before + interest + involvement)/3


gen speaker = VCF9260	// What job or office is held by named Speaker of the U.S. House
recode speaker (2=0)

gen which_party = VCF0502	// Which Major Party is More Conservative 3-Category
recode which_party (2=0) (9=.)

gen sc_justice = VCF9262	// What job or office is held by named Chief Justice of the U.S. Supreme Court
recode sc_justice (2=0)

gen vp = VCF9261	// What job or office is held by named Vice-President
recode vp (2=0)

irt 2pl speaker which_party sc_justice vp
predict info_irt, latent 
su info_irt, meanonly
replace info_irt = (info_irt- r(min)) /(r(max) - r(min))




*******************
* EQUALITARIANISM *
*******************
* Scale created from four tiems
	* 1. 'Our society should do whatever is necessary to make sure that 
			* everyone has an equal opportunity to succeed.'
	* 2 (not asked in 2016). 'We have gone too far in pushing equal rights in this country.'
	* 3 (not asked in 2016). 'One of the big problems in this country is that we don't give
			* everyone an equal chance.'
	* 4. 'It is not really that big a problem if some people have more of a
			* chance in life than others.'
	* 5. 'The (2004: This_ country would be better off if we worried less
			* about how equal people are.'		
	* 6. 'If people were treated more equally in this country we would have
			* many fewer problems.'

gen equality1 = VCF9013
recode equality1 (8=.) (9=.)
recode equality1 (1=5) (5=1) (2=4) (4=2)

gen equality2 = VCF9014 // not asked in 2016
recode equality2 (8=.) (9=.)

gen equality3 = VCF9015 // not asked in 2016
recode equality3 (8=.) (9=.)
recode equality3 (1=5) (5=1) (2=4) (4=2)

gen equality4 = VCF9016
recode equality4 (8=.) (9=.)

gen equality5 = VCF9017
recode equality5 (8=.) (9=.)

gen equality6 = VCF9018
recode equality6 (8=.) (9=.)
recode equality6 (1=5) (5=1) (2=4) (4=2)


gen equality = (equality1 + equality4 + equality5 ///
	+ equality6)/4
su equality, meanonly
replace equality = (equality- r(min)) /(r(max) - r(min))

egen equality_constraint = rsd(equality1 equality2 equality3 equality4 equality5 equality6) 
replace equality_constraint = -1 * equality_constraint


su equality_constraint, meanonly
replace equality_constraint = (equality_constraint - r(min)) ///
/(r(max) - r(min))


gen missing = 0
gen missing1 = 1 if equality1==.
replace missing1 = 0 if equality1!=.
*gen missing2 = 1 if equality2==.
*replace missing2 = 0 if equality2!=.
*gen missing3 = 1 if equality3==.
*replace missing3 = 0 if equality3!=.
gen missing4 = 1 if equality4==.
replace missing4 = 0 if equality4!=.
gen missing5 = 1 if equality5==.
replace missing5 = 0 if equality5!=.
gen missing6 = 1 if equality6==.
replace missing6 = 0 if equality6!=.
replace missing = missing1+missing4+missing5+missing6

irt grm equality1  equality4 - equality6
predict equality_irt, latent 
replace equality_irt = . if missing >0
su equality_irt, meanonly
replace equality_irt = (equality_irt- r(min)) /(r(max) - r(min))

*irt grm equality1  equality4 - equality6 if soph_cat!=., group(soph_cat)
*predict equality_dif, latent 
*replace equality_dif = . if missing >0
*su equality_dif, meanonly
*replace equality_dif = (equality_dif- r(min)) /(r(max) - r(min))

*2008 and 2012 six-item scale
irt grm equality1 - equality6 if year!=2016
predict equality_irt_alt if year!=2016, latent 
sum equality_irt_alt, meanonly
replace equality_irt_alt = (equality_irt_alt- r(min)) /(r(max) - r(min)) 
alpha equality1 - equality6

pca equality1 equality4 - equality6  
alpha equality1 equality4 - equality6  


*****************************************
* ISSUES RELATED TO GROUPS AND EQUALITY *
*****************************************
*********************
* African Americans *
*********************
*ISSUES: Aid to Blacks Scale
*	Some people say that because of past discrimination blacks should be
*		given preference in hiring and promotion.  Others say that such
*		preference in hiring and promotion of blacks is wrong because it gives
*		blacks advantages they haven't earned.  What about your opinion-- are
*		you for or against preferential hiring and promotion of blacks?

gen aid_to_blacks = VCF0830	
recode aid_to_blacks (0=.) (9=.)
revrs aid_to_black, replace	

*ISSUES: Affirmative Action in Hiring/Promotion
*	Some people say that because of past discrimination blacks should be
*		given preference in hiring and promotion.  Others say that such
*		preference in hiring and promotion of blacks is wrong because it gives
*		blacks advantages they haven't earned.  What about your opinion-- are
*		you for or against preferential hiring and promotion of blacks?

gen affirmative_action = VCF0867a		
recode affirmative_action (8=3) (7=3) (9=.)	
revrs affirmative_action, replace	


*ISSUES: Government Should Ensure Fair Jobs for Blacks
	* Only available in 1986 and 1988
gen fair_jobs = VCF9038	
recode fair_jobs (8=3.) (9=.)	
su fair_jobs, meanonly
replace fair_jobs = (fair_jobs- r(min))/(r(max) - r(min))
	
* Scale for aid to blacks/affirmative action:
set more off 
irt grm aid_to_blacks  affirmative_action , iterate(200)
predict black_issues_irt, latent 
replace black_issues_irt  = . if aid_to_blacks==.
replace black_issues_irt  = . if affirmative_action==.
su black_issues_irt, meanonly
replace black_issues_irt  = (black_issues_irt- r(min)) /(r(max) - r(min))

su aid_to_blacks, meanonly
replace aid_to_blacks = (aid_to_blacks- r(min))/(r(max) - r(min))
su affirmative_action, meanonly
replace affirmative_action = (affirmative_action- r(min))/(r(max) - r(min))
gen black_issues = (aid_to_blacks + affirmative_action) / 2
alpha aid_to_blacks affirmative_action

 alpha affirm aid_to_blacks 
 
************************************************
* Poor Americans – welfare and aid to the poor *
************************************************
gen welfare = VCF0894
recode welfare (8=.) (9=.)
revrs welfare , replace

gen aid_to_poor = VCF0886
recode aid_to_poor (8=.) (9=.)
revrs aid_to_poor , replace

* Scale for welfare/aid to the poor:
alpha welfare aid_to_poor

irt grm welfare  aid_to_poor //, iterate(200)
predict welfare_support_irt, latent 
replace welfare_support_irt = . if welfare==.
replace welfare_support_irt = . if aid_to_poor==.
su welfare_support_irt, meanonly
replace welfare_support_irt  = (welfare_support_irt- r(min)) /(r(max) - r(min))

gen welfare_support = (welfare + aid_to_poor)/2
sum welfare_support, meanonly
replace welfare_support = (welfare_support- r(min)) ///
	/(r(max) - r(min))
	

**********************************************
* LGBTQ – discrimination, military, adoption *
**********************************************
*ISSUES: Strength of Position on Law to Protect Homosexuals Against Discrimination
gen gay_discrimination = VCF0876a	
recode gay_discrimination (9=.) (8=.) (7=.) (1=4) (2=3) (4=2) (5=1)

*ISSUES: Gay military
gen gay_military = VCF0877a	
recode gay_military (9=.) (8=.) (7=.) (1=4) (2=3) (4=2) (5=1)

*ISSUES: Should Gays/Lesbians Be Able to Adopt Children
gen gay_adopt = VCF0878	
recode gay_adopt (9=.) (8=.)	
recode gay_adopt (5=0)

set more off
irt hybrid (grm gay_discrimination gay_military) (2pl gay_adopt) 
predict gay_irt, latent
replace gay_irt = . if gay_discrimination==.
replace gay_irt = . if gay_military==.
replace gay_irt = . if gay_adopt==.
su gay_irt, meanonly
replace gay_irt  = (gay_irt- r(min)) /(r(max) - r(min))

factor gay_discrimination gay_military gay_adopt , ipf
predict f1

su gay_discrimination, meanonly
replace gay_discrimination  = (gay_discrimination- r(min)) /(r(max) - r(min))
su gay_military, meanonly
replace gay_military  = (gay_military- r(min)) /(r(max) - r(min))
gen gay_issues = (gay_discrimination + gay_military + gay_adopt)/3
alpha gay_discrimination gay_military gay_adopt


*******************
* GROUP VARIABLES *
*******************
* Race
gen race = VCF0105b
recode race (0=.) (9=.)
label define racelabel 1 "White" 2 "Black" 3 "Hispanic" 4 "Other" 
label values race racelabel  
*1. White non-Hispanic
*2. Black non-Hispanic
*3. Hispanic
*4. Other or multiple races, non-Hispanic

* Gender
gen sex = VCF0104
recode sex (0=.) (3=.)
replace sex = sex-1
label define sexlabel 0 "Male" 1 "Female" 
label values sex sexlabel

gen categories = .
replace categories = 1 if sex == 1 & race==2
replace categories = 2 if sex == 1 & race==1
replace categories = 3 if sex == 0 & race==2
replace categories = 4 if sex == 0 & race==1
# delimit ; 
label define cat_label        
	1 "Black woman" 
	1 "Black woman" 
	2 "White woman" 
	3 "Black man" 
	4 "White man"          
;
# delimit cr
label values categories cat_label


*********************
* CONTROL VARIABLES *
*********************
gen mode = VCF0017
recode mode (4=1)

gen age = VCF0101
replace age = 97 if age>97

gen educ = VCF0140
replace educ = . if educ >6
recode educ (1=1) (2=2) (3=3) (4=3) (5=4) (6=5)

gen income = VCF0114
replace income = . if VCF0114 == 0
label define incomelabel 1 "0 to 16 percentile" 2 "17 to 33 percentile" 3 "34 to 67 percentile" 4 "68 to 95 percentile" 5 "96 to 100 percentile"
label values income incomelabel  
*1. 0 to 16 percentile
*2. 17 to 33 percentile
*3. 34 to 67 percentile
*4. 68 to 95 percentile
*5. 96 to 100 percentile

* Ideology
gen ideology = VCF0803
recode ideology (9=.) (0=.)

* Partisanship
gen pid = VCF0301
recode pid (0=.)
# delimit ; 
label define pid_label        
		1 "Strong Democrat" 
        2 "Weak Democrat" 
        3 "Independent - Democrat" 
        4 "Independent - Independent" 
        5 "Independent - Republican" 
		6 "Weak Republican" 
        7 "Strong Republican"
;
# delimit cr
label values pid pid_label

gen party = 0 if pid<4
replace party = 1 if pid==4
replace party = 2 if pid>4
label define partylabel 0 "Democrat" 1 "Independent" 2 "Republican" 
label values party partylabel


gen pidi = abs(pid-4)
# delimit ; 
label define pidi_label        
		0 "Independent" 
		1 "Weak partisan" 
        2 "Partisan" 
		3 "Strong partisan" 
;
# delimit cr
label values pidi pidi_label

gen democrat = 1 if pid<4

* Codes racial resentment for Table E3
*RACIAL RESENTMENT: Conditions Make it Difficult for Blacks to Succeed
gen racial_resentment1 = VCF9039	
recode racial_resentment1 (8=.) (9=.)
recode racial_resentment1 (1=5) (5=1) (4=2) (2=4)

*RACIAL RESENTMENT: Blacks Should Not Have Special Favors to Succeed
gen racial_resentment2 = VCF9040	
recode racial_resentment2 (8=.) (9=.)

*RACIAL RESENTMENT: Blacks Must Try Harder to Succeed
gen racial_resentment3 = VCF9041	
recode racial_resentment3 (8=.) (9=.)

*RACIALRESENTMENT: Blacks Gotten Less than They Deserve Over the Past Few Years
* Higher values represent more resentment; coded to range from zero to one
gen racial_resentment4 = VCF9042	
recode racial_resentment4 (8=.) (9=.)
recode racial_resentment4 (1=5) (5=1) (4=2) (2=4)

alpha racial_resentment1 racial_resentment2 racial_resentment3 racial_resentment4 

gen racial_resentment = (racial_resentment1 + racial_resentment2 + ///
	racial_resentment3 + racial_resentment4)/4

revrs racial_resentment , replace
su racial_resentment, meanonly
replace racial_resentment = (racial_resentment- r(min)) ///
	/(r(max) - r(min))


irt grm racial_resentment1 - racial_resentment4 if year>2005
predict racial_resentment_irt, latent
revrs racial_resentment_irt , replace
su racial_resentment_irt, meanonly
replace racial_resentment_irt = (racial_resentment_irt- r(min)) ///
	/(r(max) - r(min))
replace racial_resentment_irt=. if racial_resentment1==.
replace racial_resentment_irt=. if racial_resentment4==.
replace racial_resentment_irt=. if racial_resentment4==.
replace racial_resentment_irt=. if racial_resentment4==.

* Blacks lazy
gen blackslazy = VCF9271

* Whites lazy (reverse coded)
gen whiteslazy = VCF9270
*revrs whiteslazy , replace

* Hispanics lazy (reverse coded)
gen latinoslazy = VCF9272
*revrs latinoslazy , replace

gen prejudice = blackslazy-whiteslazy if race == 1
replace prejudice = blackslazy-latinoslazy if race == 3

* Black termometer
gen blacktherm = VCF0206
recode blacktherm (97=100)
*White thermometer
gen whitetherm = VCF0207
recode whitetherm (97=100)

*White thermometer
gen latinotherm = VCF0217
recode latinotherm (97=100)

* Difference
gen diftherm = whitetherm-blacktherm if race==1
replace diftherm = latinotherm-blacktherm if race==3


reg equality_irt c.soph_irt pidi i.sex income age educ i.race ideology  if year>1999 & pid<4
reg black_issues_irt ib1.race##c.soph_irt pidi age income educ sex equality_irt ideology if year>1999 & pid<4 
reg welfare_support_irt c.income##c.soph_irt pidi age  educ i.race sex equality_irt ideology if year>1999 & pid<4
reg gay_irt c.soph_irt pidi age  educ i.race sex equality_irt ideology if year>1999 & pid<4


